﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;
using Common.Utility;

namespace DataInterpolator.Model
{
   public class WellDatabase
   {
      public WellDatabase()
      {
         Wells = new ObservableDictionary<Guid, Well>();
         UnsavedChanges = false;
      }

      [XmlIgnore()]
      public string Filename
      {
         get; set;
      }

      [XmlIgnore()]
      public bool UnsavedChanges
      {
         get; set;
      }

      private ObservableDictionary<Guid, Well> _Wells = null;

      [XmlIgnore()]
      public ObservableDictionary<Guid, Well> Wells
      {
         get
         {
            return _Wells;
         }
         set
         {
            if (_Wells != null)
            {
               _Wells.CollectionChanged -= Wells_CollectionChanged;
            }

            _Wells = value;

            if (_Wells != null)
            {
               _Wells.CollectionChanged += Wells_CollectionChanged;
            }
         }
      }

      [XmlArray("Wells")]
      [XmlArrayItem("Well")]
      public Well[] WellList
      {
         get
         {
            return Wells.Values.ToArray();
         }
         set
         {
            Wells = new ObservableDictionary<Guid, Well>();

            //Now create entries for each well
            foreach (var well in value)
            {
               Wells[well.ID] = well;
            }
         }
      }

      private void Wells_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
      {
         UnsavedChanges = true;
      }

      public void Load(string filename)
      {
         if (!File.Exists(filename))
            throw new FileNotFoundException("Could not find file: " + filename);

         XmlSerializer serializer = new XmlSerializer(typeof(WellDatabase));

         using (FileStream fs = new FileStream(filename, FileMode.Open))
         {
            WellDatabase db = (WellDatabase)serializer.Deserialize(fs);

            WellList = db.WellList;
         }

         Filename = filename;
         UnsavedChanges = false;
      }

      public bool Save()
      {
         try
         {
            Save(Filename);
            return true;
         }
         catch (Exception)
         {
            return false;
         }
      }

      public void Save(string filename)
      {
         WellList = Wells.Values.ToArray();

         if (filename == null)
            throw new FileNotFoundException("No filename specified");

         XmlSerializer serializer = new XmlSerializer(typeof(WellDatabase));

         using (FileStream fs = new FileStream(filename, FileMode.Create))
         {
            serializer.Serialize(fs, this);
         }

         UnsavedChanges = false;
      }

      public Guid TitleToID(string title)
      {
         if (_Wells == null)
            return Guid.Empty;

         foreach (var well in _Wells.Values)
         {
            if (well.Title == title)
               return well.ID;
         }

         return Guid.Empty;
      }
   }
}
